package exercise.coroutine;

import java.util.concurrent.*;

/**
 * 带超时的任务（如接口调用超时控制）
 */
public class ConcurrencyJavaQ3 {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<String> future = executor.submit(() -> "线程池中执行");
        try {
            String result = future.get(3, TimeUnit.SECONDS);
        } catch (ExecutionException | InterruptedException | TimeoutException e) {
            future.cancel(true); // 超时取消
            throw new RuntimeException("调用超时");
        }
    }
}
